#include "ScreenManager.h"
#include <windows.h>
#include <iostream>
#include "KeyUtility.h"
#include "SoundManager.h"

using namespace std;

ScreenManager::ScreenManager(void)
{
	screensList = vector<IScene*>();
}


ScreenManager::~ScreenManager(void)
{
}

void ScreenManager::LoadContend()
{
	for( auto screen : screensList )
	{
		screen->LoadContend();
	}
}


void ScreenManager::UnloadContend()
{
	for( auto screen : screensList )
	{
		screen->UnloadContend();
	}
}


void ScreenManager::Update(float _dt)
{

	if(!screensList.empty())
	{
		for(int i = 0; i < screensList.size(); ++i)
		{	
			IScene* scene;
			scene = screensList[i];
			if(scene->m_screenState == Deactive)//dong
			{
				screensList.erase(screensList.begin() + i);
			}
			if(scene->m_screenState == Active)//bat
			{
				screensList.push_back(scene);
				scene->m_screenState = On;
			}
			if(scene->m_screenState == On)//dang chay
			{
				scene->Update(_dt);
			}
			if(scene->m_screenState == Off)//k chay
			{
			}

		}
	}

}

void ScreenManager::KeyDown(int keyCode)
{

	for(int i = 0; i < screensList.size(); ++i)
	{	
		IScene* scene;
		scene = screensList[i];
		if(scene->m_screenState == On)
		{
			scene->KeyDown(keyCode);
		}
	}
}
void ScreenManager::MouseLoad()
{
	
	for(int i = 0; i < screensList.size(); ++i)
	{	
		IScene* scene;
		scene = screensList[i];
		if(scene->m_screenState == On)
		{
			if(MouseUtility::GetInst()->m_isLClicked)
			{
				scene->MouseClick(0);
				MouseUtility::GetInst()->m_isLClicked = false;
			}
			if(MouseUtility::GetInst()->m_isRClicked)
			{
				scene->MouseClick(1);
				MouseUtility::GetInst()->m_isRClicked = false;
			}
		}
	}
}
void ScreenManager::KeyInput()
{
	
	for(int i = 0; i < screensList.size(); ++i)
	{	
		IScene* scene;
		scene = screensList[i];
		if(scene->m_screenState == On)
		{
			if(GIsKeyDown(GKeys::KEY_A))
			{
				scene->KeyDown(KEY_A);
			}
			if(GIsKeyDown(GKeys::KEY_W))
			{
				scene->KeyDown(KEY_W);
			}
			if(GIsKeyDown(GKeys::KEY_S))
			{
				scene->KeyDown(KEY_S);
			}
			if(GIsKeyDown(GKeys::KEY_D))
			{
				scene->KeyDown(KEY_D);
			}
			
			
			if(GIsKeyPress(GKeys::KEY_ENTER))
			{
				scene->KeyDown(KEY_ENTER);
			}
			if(GIsKeyPress(GKeys::KEY_LEFT))
			{
				scene->KeyDown(KEY_LEFT);
			}
			if(GIsKeyPress(GKeys::KEY_RIGHT))
			{
				scene->KeyDown(KEY_RIGHT);
			}
			if(GIsKeyPress(GKeys::KEY_UP))
			{
				scene->KeyDown(KEY_UP);
			}
			if(GIsKeyPress(GKeys::KEY_DOWN))
			{
				scene->KeyDown(KEY_DOWN);
			}
			if(GIsKeyPress(GKeys::KEY_SPACE))
			{
				scene->KeyDown(KEY_SPACE);
			}
			if(GIsKeyPress(GKeys::KEY_ESC))
			{
				scene->KeyDown(KEY_ESC);
			}
			

		}
	}
}
void ScreenManager::DrawScreen()
{
	for(int i = 0; i < screensList.size(); ++i)
	{	
		IScene* scene;
		scene = screensList[i];
		if(scene->m_screenState == On)
			scene->Draw();
	}
}


void ScreenManager::AddScreen(IScene* screen)
{
	screen->LoadContend();
	screensList.push_back(screen);
}


void ScreenManager::RemoveScreen(IScene* screen)
{
	for(int i=0; i < screensList.size(); i++)
	{

		if(screen == screensList[i]) 
			screensList.erase(screensList.begin() + i);
	}
}